package com.atguigu.service.impl;

import com.atguigu.dao.BookDao;
import com.atguigu.dao.impl.BookDaoImpl;
import com.atguigu.domain.Book;
import com.atguigu.service.BookService;
import com.atguigu.domain.Page;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author dl
 * @create 2020-04-25-14:44
 */
public class BookServiceImpl implements BookService {

	private BookDao bookDao = new BookDaoImpl();

	@Override
	public List<Book> getAllBook() {
		return bookDao.getAllBook();
	}

	@Override
	public boolean save(Book book) {
		return bookDao.saveBook(book);
	}

	@Override
	public boolean update(Book book) {
		return bookDao.updateBook(book);
	}

	@Override
	public boolean delete(Book book) {
		return bookDao.delBook(book);
	}

	@Override
	public Book getOneBook(Integer bookId) {
		return bookDao.getBookById(bookId);
	}

	@Override
	public Page getPage(String pageNum, String pageSize) {
		Page<Book> page = new Page<>();
		// change value type
		int pn = 1;
		int ps = page.getPageSize();
		// 可能会相互影响 所以分开来进行try - catch
		try {
			pn = Integer.parseInt(pageNum);
		} catch (Exception e) {
			System.err.println("----BookServiceImpl..getPageBook() convert exceptions----" + e.getMessage());
			// todo why?
//			ps = page.getPageSize();
		}
		try {
			ps = Integer.parseInt(pageSize);
		} catch (Exception e) {
			System.out.println();
		}
		// query total count
		Object count = bookDao.queryCount();
		int totalCount = 0;
		try {
			totalCount = Integer.parseInt(count.toString());
		} catch (Exception e) {
			System.err.println("----BookServiceImpl..getPageBook() convert exceptions----" + e.getMessage());
		}
		// 为了确保数据的正确性 需要按照顺序进行赋值 在设置当前页的时候需要用到 totalPage 的数据 而这个数据是根据 totalCount 计算出来的
		page.setTotalCount(totalCount);
		page.setCurrentPage(pn);
		page.setPageSize(ps);
		List<Book> items = bookDao.getItems(page.getIndex(), page.getPageSize());
		page.setItems(items);
		return page;
	}

	@Override
	public Page<Book> getPageByPrice(String pageNum, String pageSize, String minPrice, String maxPrice) {
		// 1 create Page<T>
		Page<Book> page = new Page<>();
		// 2 change minPrice and maxPrice to BigDeciam
		BigDecimal minChange = new BigDecimal(0);
		BigDecimal maxChange = new BigDecimal(Double.MAX_VALUE);
		// 可能会相互影响 所以分开来进行try - catch
		try {
			minChange = new BigDecimal(minPrice);
		} catch (Exception e) {
			System.err.println("----BookServiceImpl..getPageByPrice() convert exceptions minChange----" + e.getMessage());
		}
		try {
			maxChange = new BigDecimal(maxPrice);
		} catch (Exception e) {
			System.err.println("----BookServiceImpl..getPageByPrice() convert exceptions maxChange----" + e.getMessage());
		}
		// 可能出校 20 - 10 这种情况 为了防止数据库报错
		int count;
		if (maxChange.compareTo(minChange) > 0) {
			Object o = bookDao.queryCountByPrice(minChange, maxChange);
			count = Integer.parseInt(o.toString());
		}else {
			count = 0;
		}
		// 查询到带条件之后的总条数
		page.setTotalCount(count);
		// 转换 pageNum 和 pageSize  仍然为了防止用户输入错误导致相互进行影响 分开进行 try-catch

		int pn = 0;
		try {
			pn = Integer.parseInt(pageNum);
		} catch (Exception e) {
			System.err.println("----BookServiceImpl..getPageByPrice() convert exceptions pn----" + e.getMessage());
		}
		int ps = page.getPageSize();
		try {
			ps = Integer.parseInt(pageSize);
		} catch (Exception e) {
			System.err.println("----BookServiceImpl..getPageByPrice() convert exceptions ps----" + e.getMessage());
		}
		page.setCurrentPage(pn);
		page.setPageSize(ps);
		// 查询book items
		List<Book> items = bookDao.getItemsByPrice(page.getIndex(), page.getPageSize(), minChange, maxChange);
		page.setItems(items);
		return page;
	}
}
